package com.example.yrdev.ordering_system.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yrdev.ordering_system.Database.DingHanDB;
import com.example.yrdev.ordering_system.Model.CardRecord;
import com.example.yrdev.ordering_system.Model.CustomViewHolder;
import com.example.yrdev.ordering_system.Model.PurchaseReport;
import com.example.yrdev.ordering_system.R;

import java.util.ArrayList;

/**
 * Created by luke on 10/30/17.
 */

public class ReportMemberAdapter extends BaseAdapter {

    Context context;
    ArrayList arrayList;

    public ReportMemberAdapter(Context context, ArrayList arrayList){

        this.context=context;
        this.arrayList=arrayList;

    }

    public void refresh(ArrayList arrayList){
        this.arrayList=arrayList;
        notifyDataSetChanged();

    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        LayoutInflater inflater=  LayoutInflater.from(context);
        Object object=arrayList.get(i);

        if (view==null){

            view=inflater.inflate(R.layout.member_report_row,null);
            CustomViewHolder viewHolder=new CustomViewHolder();

            viewHolder.setCard_no((TextView)view.findViewById(R.id.card_no));
            viewHolder.setCard_usageCount((TextView)view.findViewById(R.id.card_usage_count));
            viewHolder.setCard_purchase((TextView)view.findViewById(R.id.card_purchase_amount));
            viewHolder.setCard_remain((TextView)view.findViewById(R.id.card_remain));
            viewHolder.setCard_date((TextView)view.findViewById(R.id.card_date));
            viewHolder.setCard_Operator((TextView)view.findViewById(R.id.card_operator));
            viewHolder.setIndex((TextView)view.findViewById(R.id.index));
            viewHolder.setTick((ImageView)view.findViewById(R.id.tick));
            viewHolder.setUploadMark((TextView)view.findViewById(R.id.uploadMark));
            view.setTag(viewHolder);
        }

        CustomViewHolder viewHolder=(CustomViewHolder)view.getTag();

        CardRecord respRecord=null;

        if (object instanceof CardRecord){

            respRecord=(CardRecord)object;

        }else if(object instanceof PurchaseReport){

            PurchaseReport report=(PurchaseReport)object;

            respRecord=  DingHanDB.getInstance(context).getCorresPondReport(report);

            //   GlobalFunction.printTextByTag("ID:"+((PurchasedItemTB.Item) object).getItem_TicketID());
        }

//            if (respRecord.getUploadMark()==1){
//
//                viewHolder.getTick().setVisibility(View.VISIBLE);
//
//            }else {
//                viewHolder.getTick().setVisibility(View.GONE);
//            }


        viewHolder.getIndex().setText(i+1+"");
        passValueToViewHolder(viewHolder,respRecord);

        return view;


    }


    public void passValueToViewHolder(CustomViewHolder viewHolder, CardRecord record){
        try {
            viewHolder.getCard_no().setText(record.getCardNo());
            viewHolder.getCard_usageCount().setText(String.valueOf(record.getUsageCount()));
            viewHolder.getCard_purchase().setText(String.valueOf(record.getPurchaseAmount())+"元");
            viewHolder.getCard_remain().setText(String.valueOf(record.getRemain())+"元");
            viewHolder.getCard_date().setText(record.getUsageDate());
            viewHolder.getCard_Operator().setText(record.getOperator());


            if (record.getUploadMark()==0)
                viewHolder.getUploadMark().setText("未上传");
            else
                viewHolder.getUploadMark().setText("已上传");



        }catch (Exception e){
            // GlobalFunction.printTextByTag(e.getMessage());
        }

    }



}
